<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>script 查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-script-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-script-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-script-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-script-query.html" rel="nofollow" target="_blank">../en/query-dsl-script-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="specialized-queries.html">专业的查询</a></span>
»
<span class="breadcrumb-node">script 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-dsl-rank-feature-query.html">« rank_feature 查询</a>
</span>
<span class="next">
<a href="query-dsl-script-score-query.html">script_score 查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-script-query"></a>script 查询
</h2>
</div></div></div>

<p>
基于指定的 <a class="xref" href="modules-scripting-using.html" title="如何使用脚本" rel="nofollow">script</a> 过滤文档。

<code class="literal">script</code> 查询通常在 <a class="xref" href="query-filter-context.html" title="查询和filter上下文">filter上下文</a>中使用。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="script-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "bool" : {
            "filter" : {
                "script" : {
                    "script" : {
                        "source": "doc['num1'].value &gt; 1",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/258.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="script-top-level-params"></a><code class="literal">script</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">script</code>
</span>
</dt>
<dd>
(必需, <a class="xref" href="modules-scripting-using.html" title="如何使用脚本" rel="nofollow">script object</a>) 包含作为查询运行的脚本。

这个脚本必须返回一个布尔值，<code class="literal">true</code> 或 <code class="literal">false</code>。
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="script-query-notes"></a>注意
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="script-query-custom-params"></a>自定义参数
</h4>
</div></div></div>
<p>
像 <a class="xref" href="query-filter-context.html" title="Query and filter context">filters</a> 一样，脚本被缓存以加快执行速度。

如果经常更改脚本的参数，建议将它们存储在脚本的 <code class="literal">params</code> 参数中。

例如:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "bool" : {
            "filter" : {
                "script" : {
                    "script" : {
                        "source" : "doc['num1'].value &gt; params.param1",
                        "lang"   : "painless",
                        "params" : {
                            "param1" : 5
                        }
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/259.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_allow_expensive_queries_4"></a>允许执行昂贵的查询
</h4>
</div></div></div>
<p>
如果 <a class="xref" href="query-dsl.html#query-dsl-allow-expensive-queries"><code class="literal">search.allow_expensive_queries</code></a> 设置为 <code class="literal">false</code>，脚本查询不会被执行。
</p>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="query-dsl-rank-feature-query.html">« rank_feature 查询</a>
</span>
<span class="next">
<a href="query-dsl-script-score-query.html">script_score 查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>